﻿using PLM.ConfigurationCenter.Handler;
using PLM.ConfigurationCenter.Model;
using PLM.ConfigurationCenter.SqlSugar;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PLM.ConfigurationCenter {
    public partial class StartForm : UIForm {
        public StartForm() {
            InitializeComponent();
        }

        private void StartForm_Load(object sender, EventArgs e) {
            var dbmodel = DbManager.DbContext.Queryable<DatabaseInfoModel>().First(p => p.Id == "AuxiliaryDb");
            BindData(dbmodel);
        }
        /// <summary>
        /// 
        /// </summary>
        private void BindData(DatabaseInfoModel model) {
            if (model == null) {
                this.tb_Host.Text = ServerHandler.GetLocalIP();
                this.tb_Port.Text = "5432";
            } else {
                this.tb_DBName.Text = model.DbName;
                this.tb_DBPassword.Text = model.Password;
                this.tb_DBUser.Text = model.UserName;
                this.tb_Host.Text = model.IPAddress;
                this.tb_Port.Text = model.Port.ToString();
            }
        }
        /// <summary>
        /// 
        /// </summary>
        private bool CheckData() {
            var host = this.tb_Host.Text;
            if (host.IsNullOrEmpty()) {
                MessageBox.Show("主机名不能为空！");
                return false;
            }
            var port = this.tb_Port.Text;
            if (port.IsNullOrEmpty()) {
                MessageBox.Show("端口号不能为空！");
                return false;
            }
            var dbname = this.tb_DBName.Text;
            if (dbname.IsNullOrEmpty()) {
                MessageBox.Show("数据库不能为空！");
                return false;
            }
            var dbUser = this.tb_DBUser.Text;
            if (dbUser.IsNullOrEmpty()) {
                MessageBox.Show("用户名不能为空！");
                return false;
            }
            var dbpassword = this.tb_DBPassword.Text;
            if (dbpassword.IsNullOrEmpty()) {
                MessageBox.Show("密码不能为空！");
                return false;
            }
            return true;
        }

        /// <summary>
        /// 测试连接
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_Connect_Click(object sender, EventArgs e) {
            if (!CheckData()) {
                return;
            }
            ShowWaitForm();
            var check = TestConnect(out string msg);
            if (!check) {
                HideWaitForm();
                MessageBox.Show(msg);
            } else {
                HideWaitForm();
                MessageBox.Show("连接成功！");
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        private bool TestConnect(out string msg) {
            var str = $"PORT={this.tb_Port.Text};DATABASE={this.tb_DBName.Text};HOST={this.tb_Host.Text};PASSWORD={this.tb_DBPassword.Text};USER ID={this.tb_DBUser.Text};";
            var check = CommonServerHelper.CheckPgDatabaseHealth(str, out msg);
            return check;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_Save_Click(object sender, EventArgs e) {
            if (!CheckData()) {
                return;
            }
            ShowWaitForm();
            var check = TestConnect(out string msg);
            if (!check) {
                HideWaitForm();
                MessageBox.Show(msg);
            } else {
                var dbmodel = DbManager.DbContext.Queryable<DatabaseInfoModel>().First(p => p.Id == "AuxiliaryDb");
                int res = 0;
                if (dbmodel == null) {
                    dbmodel = new DatabaseInfoModel {
                        DBType = 3, Id = "AuxiliaryDb",
                        IPAddress = this.tb_Host.Text,
                        Password = this.tb_DBPassword.Text,
                        Port = this.tb_Port.Text.ToInt(),
                        UserName = this.tb_DBUser.Text,
                        DbName = this.tb_DBName.Text
                    };
                    res = DbManager.DbContext.Insertable(dbmodel).ExecuteCommand();
                } else {
                    dbmodel.IPAddress = this.tb_Host.Text;
                    dbmodel.Password = this.tb_DBPassword.Text;
                    dbmodel.Port = this.tb_Port.Text.ToInt();
                    dbmodel.UserName = this.tb_DBUser.Text;
                    dbmodel.DbName = this.tb_DBName.Text;
                    res = DbManager.DbContext.Updateable(dbmodel).ExecuteCommand();
                }
                HideWaitForm();
                if (res > 0) {
                    MessageBox.Show("保存成功！");
                   this.DialogResult = DialogResult.OK;
                }
            }
        }
    }
}
